#region imported Namespaces
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using BO = PHSRAG.Insight21.BusinessObjects;
using DataAccess = PHSRAG.Utility.DataAccess;
using ICachePolicy = PHSRAG.Insight21.Policy.ICachePolicy;
#endregion

namespace PHSRAG.Insight21.Personnel
{
	/// <summary>
	/// The payrollReport object supports loading of payroll information for all the personnel 
	/// to which the user has access to.
	/// It also contains a static method for retrieving the payroll run dates.
	/// </summary>
	public class PayrollReport : BO.BusinessObject
	{
		#region Constants
		private const string PayrollReportKey = "PayrollReport_0214EC73-440E-4775-B126-24939D6030C5";
		private const string PayrollReportTableName = "PayrollReport";
		private const string PayrollReportDatesTableName ="PayrollReportDates";
		private const int PayrollDataTableIndex =0;
		private const int PayrollRunDatesTableIndex= 0;
		private const int	DomainUserIdArgumentIndex	= 1;
		private const int	ReportDateArgumentIndex =0;	
		#endregion

		# region Constructor
		/// <summary>
		/// Initialize the base class with the specified cache policy.
		/// </summary>
		/// <param name="cachePolicy">The prevalent cache policy</param>
		public PayrollReport() : base(null)
		{}
		#endregion

		#region Public properties
		/// <summary>
		/// Get the key to be used in all caching operations for this instance of the object.
		/// </summary>
		public static string CacheKey	{ get { return PayrollReportKey; } }
		#endregion

		#region Class Methods
		/// <summary>
		/// This method loads the list of Payroll Report Dates.
		/// This method doesn't take any arguments and hence null is passed in the GetDataSet method.
		/// </summary>
		/// <returns>DataTable containing the list of payroll report dates.</returns>
		public static DataTable GetPayrollReportDates()
		{
			DataSet payrollReportDates = DataAccess.GetDataSet(DataAccess.ConnectionString,	Constants.GetPayrollReportDatesSPROCName,null);

			if (payrollReportDates.Tables.Count ==1)
				payrollReportDates.Tables[PayrollRunDatesTableIndex].TableName = PayrollReportDatesTableName;		
			else
				throw new Exception("Error occurred while loading the Payroll Report dates.");
			
			return payrollReportDates.Tables[PayrollRunDatesTableIndex];
		}
		#endregion

		#region Business Object Methods
		/// <summary>
		/// Load the DataSet with payroll report data by executing the stored procedure
		/// "GetPayrollReport".  Throws system exception if an error is encountered 
		/// during the load process.
		/// Required parameters to the stored procedure are a)ReportDate and b)Domain User ID
		/// </summary>
		/// <param name="connectionString">Sql Connection string</param>
		/// <param name="args">Arguments to store procedure - ReportDate and DomainuserID</param>
		public override void Load(string connectionString, params object[] args)
		{
			if (args.Length != 2)
				throw new Exception("Payroll Report load requires Report Date and Domain User ID parameters.");

			DataContainer =	DataAccess.GetDataSet(connectionString, Constants.GetPayrollReportSPROCName,
				DataAccess.BuildSqlParameterArray(
				"@reportDate",SqlDbType.VarChar,args[ReportDateArgumentIndex]
				,"@domainUserID", SqlDbType.Int, (int)args[DomainUserIdArgumentIndex]));

			// guard against the case where no rows are returned
			if (DataContainer.Tables.Count == 0)
			{
				DataContainer.Tables.Add(PayrollReportTableName);
			}
			else if (DataContainer.Tables.Count ==1)
			{
				DataContainer.Tables[PayrollDataTableIndex].TableName = PayrollReportTableName;
			}
			else
				throw new Exception("Error occurred while loading the Payroll data");
		}
		#endregion
	}
}
